package util.cleaner;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Hashtable;

import util.io.FileInput;

public class TrimByFrequency {
	/**
	 * @param args
	 */
	
	static private int _id_url = 0;
	static private int _id_tag = 1;
	static private int _url = 2;
	static private int _tag = 3;
	static private int _freq_u = 5;
	static private int _freq_tag = 4;
	static private int _freq_ut = 6;
	static private int _age = 7;
	
	int offset= 10000000;
	
	public static void trimByFreq(String path, int min_t, int min_adults){
		
		FileInput in = new FileInput(path);
		String line = in.readString();
		int ids = 0;

		Hashtable<String, Integer> urls_h = new Hashtable<String, Integer>();

		Hashtable<String, Integer> tags_h = new Hashtable<String, Integer>();

		// add url ids

		while (line != null) {

			String[] fields = line.split("\t");

			Integer id_url = Integer.valueOf(Integer.valueOf(fields[_id_url]));
			Integer id_tag = Integer.valueOf(Integer.valueOf(fields[_id_tag]));

			Integer freq_tag = Integer.valueOf(Integer
					.valueOf(fields[_freq_tag]));
			Integer freq_url = Integer
					.valueOf(Integer.valueOf(fields[_freq_u]));
			Integer freq_ut = Integer
					.valueOf(Integer.valueOf(fields[_freq_ut]));
			String age = fields[_age];
			String name = fields[_tag];
			String url_name = fields[_url];

			if (!tags_h.containsKey(name) &&  age.contains("adults")) {

				tags_h.put(name, 1);
				

			} else if(tags_h.containsKey(name) &&  age.contains("adults")) {
				Integer i = tags_h.get(name);
				tags_h.put(name,i+1);
			}

			line = in.readString();
		}
		
		
		//regerate file
		
		in = new FileInput(path);
		line= in.readString();
	
		while(line!=null){

			String[] fields = line.split("\t");

			Integer id_url = Integer.valueOf(Integer.valueOf(fields[_id_url]));
			Integer id_tag = Integer.valueOf(Integer.valueOf(fields[_id_tag]));

			Integer freq_tag = Integer.valueOf(Integer.valueOf(fields[_freq_tag]));
			Integer freq_url = Integer.valueOf(Integer.valueOf(fields[_freq_u]));
			Integer freq_ut = Integer.valueOf(Integer.valueOf(fields[_freq_ut]));
			String age = fields[_age];
			String name = fields[_tag];
			String url_name = fields[_url];
			

			if( !age.contains("kids")){
				
				
				int freq_adults = 0;
				if(tags_h.containsKey(name)){
					
					freq_adults = tags_h.get(name);
				}
				
			
			System.out.println(id_url + "\t"+ id_tag + "\t"+ url_name + "\t"+ name +"\t"
					+freq_tag + "\t"+ freq_url + "\t"+ freq_ut + "\t"+ age);
			
			}
			line=in.readString();
			
		}
			
			
		
		
		
		
	}
	public static void main(String[] args) throws MalformedURLException {
		// TODO Auto-generated method stub
		String prefix = "/Users/sergioduarte/projects/data/graph/";
		
		//String path = "/media/sata__/data/delicious_graph/delicious_graphML_raw_ids_dmoz.txt";
		String path = prefix+ "delicious_graphML_raw_ids_dmoz.txt";
		int min_adults=5;
		int min_teens=2;
		trimByFreq(path,2,5);

	}

}
